package com.company.project.modular.caigou.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.company.project.core.base.controller.BaseController;
import com.company.project.core.log.LogObjectHolder;
import com.company.project.modular.system.model.Purchase;
import com.company.project.modular.system.model.Standard;
import com.company.project.modular.system.service.IAuditService;
import com.company.project.modular.system.model.Audit;
import com.company.project.modular.system.service.IAuditService;
import com.company.project.modular.system.service.IStandardService;
import com.company.project.modular.system.warpper.AuditWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 领导审核控制器
 *
 * @author fengshuonan
 * @Date 2018-10-30 15:32:36
 */
@Controller
@RequestMapping("/audit")
public class AuditController extends BaseController {
    private String PREFIX = "/caigou/audit/";

    @Reference
    private IAuditService auditService;
    @Reference
    private IStandardService standardService;
    /**
     * 跳转到领导审核首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "audit.html";
    }

    /**
     * 跳转到添加领导审核
     */
    @RequestMapping("/audit_add")
    public String auditAdd() {
        return PREFIX + "audit_add.html";
    }

    /**
     * 跳转到修改领导审核
     */
    @RequestMapping("/audit_update/{auditId}")
    public String auditUpdate(@PathVariable Integer auditId, Model model) {
        Audit audit = auditService.selectById(auditId);
        model.addAttribute("item",audit);
        LogObjectHolder.me().set(audit);
        return PREFIX + "audit_edit.html";
    }

    /**
     * 获取领导审核列表(可根据关键字查询)
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        /*System.out.print("condition"+condition);*/
        if(condition==null){
            List<Map<String, Object>> audit = this.auditService.list(condition);
            return super.warpObject(new AuditWarpper(audit));
        }else{
            List<Map<String, Object>> audit = this.auditService.passList(condition);
            return super.warpObject(new AuditWarpper(audit));
        }
    }

    /**
     * 新增领导审核
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Audit audit, BindingResult bindingResult) {
        String maxNumber = String.valueOf(auditService.findByMaxNumber().get(0).getAuditId());
        if("null".equals(maxNumber)){
            maxNumber = null;
            int a = 30001;
            String number = String.valueOf(a);
            audit.setAuditId(number);
        }else {
            int a = Integer.valueOf(maxNumber)+1;
            String number = String.valueOf(a);
            audit.setAuditId(number);
        }
        auditService.insert(audit);
        return SUCCESS_TIP;
    }

    /**
     * 删除领导审核
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer auditId) {
        auditService.deleteById(auditId);
        return SUCCESS_TIP;
    }

    /**
     * 修改领导审核
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Audit audit, Standard standard, Purchase purchase, BindingResult bindingResult) {
        /*审核人==登录系统的人的名称*/
        super.getSession().getAttribute("username");//admin
        Object name = super.getSession().getAttribute("name");  //张三
        String persion = String.valueOf(name);

        /*标准物质表*/
        /*通过并且属于采购类型*/
        if(audit.getAuditPass()==1 && audit.getAuditState()==2){
            /*获取到的名称是否存在于标准物质表里面，不存在则添加，存在则不需要再添加*/
            List<String> standardNameList = standardService.findByStandardName();
            if(standardNameList.contains(audit.getAuditName())&& !audit.getAuditName().equals(null)){
                System.out.println("该标准物质已经存在，不需要重复添加");
            }else{
                /*保存StandardId*/
                //得到long类型当前时间
                long l = System.currentTimeMillis(); //new日期对象
                Date date = new Date(l); //转换提日期输出格式
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                String createdate = dateFormat.format(date);
                standard.setStandardId(createdate);
                /*保存StandardLabel*/
                String maxNumber1 = String.valueOf(standardService.findByMaxNumber().get(0).getStandardLabel());
                if("null".equals(maxNumber1)){
                    int a = 50001;
                    String number1 = String.valueOf(a);
                    standard.setStandardLabel(number1);
                }else {
                    int a = Integer.valueOf(maxNumber1)+1;
                    String number1 = String.valueOf(a);
                    standard.setStandardLabel(number1);
                }
                /*保存StandardCode*/
                String standardCode =  standard.getStandardId() + standard.getStandardLabel();
                standard.setStandardCode(standardCode);

                standard.setStandardName(audit.getAuditName());//标准物质名称=审核名称=采购名称=报买名称
                /*根据输入的名称是否含有检品来判断是否是检品类型*/
                if(audit.getAuditName().contains("检品")){
                    standard.setStandardType(2);//检品
                }else{
                    standard.setStandardType(1);   //对照品
                }
                standard.setStandardOrigin(purchase.getPurSupplier());//来源=采购商
                standard.setStandardSize(purchase.getPurSpec());//规格
                standard.setStandardPersion(purchase.getPurPic());//负责人
                standard.setStandardType(2);//检品
                standardService.insert(standard);
            }
        }

        audit.setAuditPersion(persion);

        auditService.updateById(audit);
        return SUCCESS_TIP;
    }

    /**
     * 领导审核详情
     */
    @RequestMapping(value = "/detail/{auditId}")
    @ResponseBody
    public Object detail(@PathVariable("auditId") Integer auditId) {
        return auditService.selectById(auditId);
    }
}
